/* 
    Squirrel open project
    Copyright (C) 2012  Elias Aran Paul Farhan
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <squirrel/animation.hpp>
Animation::Animation()
{
	up = false;
	down = false;
	right = false;
	left = false;
		
	currentImg = 0;
	currentState = 0;
}
Animation::~Animation()
{
	//remove the states
	for (std::vector<State>::iterator it = states.begin(); it != states.end(); it++)
	{
		for (std::vector<Box*>::iterator itbox = (*it).boxes.begin(); itbox != (*it).boxes.end(); itbox++)
		{
			delete *(itbox);
		}
	} 
}
int Animation::next()
{
	int index = states[currentState].index;
	int frame0 = states[currentState].frames[0];
	int frame1 = states[currentState].frames[1];
	int frame2 = states[currentState].frames[2];
	int frame3 = states[currentState].frames[3];
	if (left && right && !up && !down)
	{
		currentImg = currentImg;
	}
	else if (right)
	{
		if (currentImg >= index + frame0)
		{
			currentImg = index;
		}
		else
		{
			currentImg = index + ((currentImg + 1)%frame0);
		}
	}
	else if (left)
	{
		if ((currentImg < index + frame0) ||
			(currentImg  >= index + frame0 + frame1))
		{
			currentImg = index + frame0;
		}
		else
		{
			currentImg = index + frame0 + ((currentImg + 1) %frame1);
		}
	}
	else if (up and down)
	{
		currentImg = currentImg;
	}
	else if (up)
	{
		if ((currentImg < index + frame0 + frame1) ||
				(currentImg  >= index + frame0 + frame1 + frame2))
		{
			currentImg = index + frame0 + frame1;
		}
		else
		{
			currentImg = index + frame0 + frame1 +((currentImg + 1) %frame2);
		}
	}
	else if (down)
	{
		if ((currentImg < index + frame0 + frame1 + frame2) || 
			(currentImg  >= index + frame0 + frame1 + frame2 + frame3))
		{
			currentImg = index + frame0 + frame1 + frame2;
		}
		else
		{
			currentImg = index + frame0 + frame1 + frame2 + ((currentImg + 1) %frame3);
		}
	}
	else
	{
		currentImg = currentImg;
	}
	return currentImg;
}
void Animation::setDirection(bool keys[4])
{
	right = keys[0];
	left = keys[1];
	up = keys[2];
	down = keys[3];
}
std::vector<Box*>* Animation::getBoxes()
{
	return &(states[currentState].boxes);
}
Point Animation::getSpeed()
{
	return std::make_pair(states[currentState].speed*(right-left), 
			states[currentState].speed*(down-up));
}
void Animation::addState(const char* name, int speed, int frames[4], int index)
{
	State state;
	state.name = name;
	state.speed = speed;
	for(int i = 0; i < 4; i++)
		state.frames[i] = frames[i];
	state.index = index;
	states.push_back(state);
}
void Animation::addBox(Box* b, int state)
{
	states[state].boxes.push_back(b);
}
void Animation::setState(int newstate)
{
	currentState = newstate;
}
int Animation::getNumState()
{
	return states.size();
}
